//
//  SVTableView.m
//  3333
//
//  Created by sujeking on 17/4/1.
//  Copyright © 2017年 szw. All rights reserved.
//

#import "SVTableView.h"
#import "UIScrollView+HXSPullRefresh.h"

@interface SVTableView() <UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *noDataView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (copy, nonatomic) void (^refreshCallBack)();
@end

@implementation SVTableView

- (instancetype)initWithRefreshCallBack:(void (^)())block {
    UINib *nib = [UINib nibWithNibName:@"SVTableView" bundle:[self resBundle]];
    self = [nib instantiateWithOwner:self options:nil].firstObject;
    if (self) {
        _refreshCallBack = block;
        [self setupSubViews];
        self.frame = (CGRect){0,0,[self screenWidth],[self screenHeight]};
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setupSubViews {
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView addRefreshHeaderWithCallback:self.refreshCallBack];
    self.frame = (CGRect){0,0,[self screenWidth],[self screenHeight]};
    
    UIImageView *noDataImgv = [[UIImageView alloc] initWithFrame:CGRectZero];
    noDataImgv.contentMode = UIViewContentModeScaleAspectFill;
    noDataImgv.image = [UIImage imageWithContentsOfFile:[[self resBundle] pathForResource:@"noData" ofType:@"png"]];
    noDataImgv.frame = (CGRect){0,100,[self screenWidth],[self screenWidth]};
    self.noDataView.backgroundColor = [UIColor whiteColor];
    [self.noDataView addSubview:noDataImgv];
}

// MARK: - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataSource svtableview:self numberOfRowsInSection:section];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if ([self.dataSource numberOfSectionsInTableView:self] == 0) {
        self.tableView.hidden = YES;
        return 0;
    } else {
        self.tableView.hidden = NO;
        return [self.dataSource numberOfSectionsInTableView:self];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   UITableViewCell *cell = [self.dataSource svtableview:self cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc] init];
    }
    return cell;
}

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return nil;
}

- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    return nil;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

// MARK: - UITableViewDelegate

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    
}
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section {
    
}

// Variable height support

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(tableView:heightForRowAtIndexPath:)]) {
        return [self.delegate tableView:self heightForRowAtIndexPath:indexPath];
    } else {
        return 44;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if ([self.delegate respondsToSelector:@selector(tableView:heightForHeaderInSection:)]) {
        return [self.delegate tableView:self heightForHeaderInSection:section];
    } else {
        return 1;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if ([self.delegate respondsToSelector:@selector(tableView:heightForFooterInSection:)]) {
        return [self.delegate tableView:self heightForFooterInSection:section];
    } else {
        return 1;
    }
}

// Section header & footer information. Views are preferred over title should you decide to provide both

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.delegate tableView:self didSelectRowAtIndexPath:indexPath];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath  {
    
}

- (void)reloadData {
    [self.tableView reloadData];
}

- (void)endRefreshing {
    [self.tableView endRefreshing];
}

- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier {
    return [self.tableView dequeueReusableCellWithIdentifier:identifier];
}

- (CGFloat)screenWidth {
    return CGRectGetWidth([[UIScreen mainScreen] bounds]);
}

- (CGFloat)screenHeight {
    return CGRectGetHeight([[UIScreen mainScreen] bounds]);
}

- (NSBundle *)resBundle {
    NSString *path = [[NSBundle mainBundle] pathForResource:NSStringFromClass([self class]) ofType:@"bundle"];
    return [NSBundle bundleWithPath:path];
}

// MARK: - Action

- (IBAction)noDataViewTapAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(tableView:noDataTapAction:)]) {
        [self.delegate tableView:self noDataTapAction:sender];
    }
}

// MARK: - GET && SET

- (void)setDataSource:(id<SVTableViewDataSource>)dataSource {
    _dataSource = dataSource;
}

- (void)setDelegate:(id<SVTableViewDelegate>)delegate {
    _delegate = delegate;
    
    UIViewController *vc = [self visibleViewController];
    if (vc.parentViewController != nil && [vc.parentViewController isKindOfClass:[UINavigationController class]]) {
        self.center = (CGPoint){self.center.x,self.center.y + 64};
    }
}

- (UIViewController *)visibleViewController {
    UIViewController *rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
    return [self getVisibleViewControllerFrom:rootViewController];
}

- (UIViewController *)getVisibleViewControllerFrom:(UIViewController *) vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self getVisibleViewControllerFrom:[((UINavigationController *) vc) visibleViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self getVisibleViewControllerFrom:[((UITabBarController *) vc) selectedViewController]];
    } else {
        if (vc.presentedViewController) {
            return [self getVisibleViewControllerFrom:vc.presentedViewController];
        } else {
            return vc;
        }
    }
}

@end
